package com.hndct.service.email;

import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: ump
 * @PachageName: com.hndct.service
 * @Author: YuZhenDong
 * @Discription: TODO
 * @Date: 2019/4/3
 * ClassName: MailService
 */
@Service
public class MailService {
    private static final Logger logger = LoggerFactory.getLogger(MailService.class);

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")  //发送人的邮箱
    private String from;

    @Autowired
    private FreeMarkerConfigurer configurer;


    /**
     * 功能描述:
     * @param title
	* @param url
	* @param email:
     * @Return: void
     * @Auther: YuZhenDong
     * @Date: 2019/4/3
     * @Discription: 发送普通邮件
     */
    @Async
    public void sendMail(String title, String url, String email) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setSubject(title);
            message.setTo(email);
            message.setText(url);
            mailSender.send(message);
        } catch (MailException e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能描述:
     * @param toUser
	* @param content:
     * @Return: void
     * @Auther: YuZhenDong
     * @Date: 2019/4/3
     * @Discription: 发送模板邮件
     */
    public String sendMessageMail(String toUser,String content) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(from);
            helper.setTo(InternetAddress.parse(toUser));
            helper.setSubject("BITDEER注册验证码");

            Map<String, Object> model = new HashMap<>();
            model.put("username", toUser);
            model.put("content", content);
            try {
                Template template = configurer.getConfiguration().getTemplate("message.ftl");
                try {
                    String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
                    helper.setText(text, true);
                    mailSender.send(mimeMessage);
                    logger.info("向用户{}发送邮件成功，内容{}",toUser,content);
                    return "向用户:"+toUser+"发送邮件成功，内容："+content;
                } catch (TemplateException e) {
                    logger.info("向用户{}发送邮件失败,原因{}",toUser,e);
                    return "发送邮件失败";
                }
            } catch (IOException e) {
                logger.info("向用户{}发送邮件失败,原因{}",toUser,e);
                return "发送邮件失败";
            }
        } catch (MessagingException e) {
            logger.info("向用户{}发送邮件失败,原因{}",toUser,e);
            return "发送邮件失败";
        }
    }
}
